package org.spbsu.timetable.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Univercity {

	private Integer pairsPerDay;
	private Integer daysPerWeek;
	private Integer audienceQnt;
	private List<Lesson> pairs = new ArrayList<>();
	private Timetable timetable;

	public Timetable getTimetable() {
		return timetable;
	}

	public void setTimetable(Timetable timetable) {
		this.timetable = timetable;
	}

	public Univercity(Integer pairsPerDayLimit, Integer daysPerWeekLimit,
			Integer audienceQnt, List<Lesson> occupations) {
		super();
		this.pairsPerDay = pairsPerDayLimit;
		this.daysPerWeek = daysPerWeekLimit;
		this.audienceQnt = audienceQnt;
		this.pairs = occupations;

		timetable = new Timetable(daysPerWeekLimit, pairsPerDayLimit);
	}

	public Integer getAudienceQnt() {
		return audienceQnt;
	}

	public void setAudienceQnt(Integer audienceQnt) {
		this.audienceQnt = audienceQnt;
	}

	public List<Lesson> getOccupations() {
		return pairs;
	}

	public void setOccupations(List<Lesson> occupations) {
		this.pairs = occupations;
	}

	public Integer getPairsPerDay() {
		return pairsPerDay;
	}

	public void setPairsPerDay(Integer pairsPerDay) {
		this.pairsPerDay = pairsPerDay;
	}

	public Integer getDaysPerWeek() {
		return daysPerWeek;
	}

	public void setDaysPerWeek(Integer daysPerWeek) {
		this.daysPerWeek = daysPerWeek;
	}

	public Integer getTeacherPairsQnt(String teacher) {
		Integer result = 0;

		for (Lesson occupation : this.getOccupations()) {
			if (occupation.getTeacher().equals(teacher)) {
				result++;
			}
		}

		return result;
	}

	public Integer getStudentPairsQnt(String group) {
		Integer result = 0;

		for (Lesson occupation : this.getOccupations()) {
			if (occupation.getGroup().equals(group)) {
				result++;
			}
		}

		return result;
	}

	public void fillTimetable() {
		Collections.sort(pairs, new LibertyComparator());

		for (Lesson pair : pairs) {
			timetable.addPair(pair);
		}
	}

}

class LibertyComparator implements Comparator<Lesson> {

	@Override
	public int compare(Lesson o1, Lesson o2) {
		if (o1.getLiberty() > o2.getLiberty()) {
			return 1;
		} else if (o1.getLiberty() < o2.getLiberty()) {
			return -1;
		} else {
			return 0;
		}
	}
}
